<?php
class BlueBarOffer extends Model_Abstract
{
	public function getAllBlueBarOffers() {
		$sql = 'SELECT * FROM bluebar_offer WHERE deleted=0 ORDER BY ord DESC';
		try {
			return $this->_connection->query($sql);
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getBlueBarOfferById ($id) {
		$sql = 'SELECT * FROM bluebar_offer WHERE id=? AND deleted=0';
		try {
			$reuslt = $this->_connection->query($sql, array($id));
			if (count($reuslt) > 0) {
				return $reuslt[0];
			}
			return null;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function addBlueBarOffer($siteId, $PartnerName, $offerText, $credit, $link, $ord, $isPartial) {
		$sql = 'INSERT INTO bluebar_offer SET site_id=?, credit=?, partner_name=?, offer_text=?, update_time=?, link=?, ord=?, is_partial=?';
		try {
			return $this->_connection->execute($sql, array($siteId, $credit, $PartnerName, $offerText, time(), $link, $ord, $isPartial));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function updateBlueBarOffer($siteId, $PartnerName, $offerText, $credit, $id, $link, $ord, $isPartial) {
		$sql = 'UPDATE bluebar_offer SET site_id=?, credit=?, partner_name=?, offer_text=?, update_time=?, link=?, ord=?, is_partial=? WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($siteId, $credit, $PartnerName, $offerText, time(), $link, $ord, $isPartial, $id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function deleteBlueBarOffer($id) {
		$sql = 'UPDATE bluebar_offer SET deleted=1 WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function pauseBlueBarOffer($id) {
		$sql = 'UPDATE bluebar_offer SET pause=(pause+1)%2 WHERE id=?';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function changeBlueBarOfferOrd($id, $addSub = 'add') {
		$sql = ' SELECT ord FROM bluebar_offer WHERE id=? ';
		try {
			$result = $this->_connection->query($sql, array($id));
			if(count($result) > 0){
				$ord = $result[0]['ord'];
				if($addSub == 'sub'){
					$ord = ($ord-1 == 0)? 10 : $ord-1;
				}else{
					$ord = ($ord % 10) + 1;
				}
				$sql = ' UPDATE bluebar_offer SET ord=? WHERE id=? ';
				if($this->_connection->execute($sql, array($ord, $id))){
					return $ord;
				}else{
					return false;
				}
			}
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
}